// pages/person/record/index.js
const app = getApp()
import Toast from "../../../vant/weapp/toast/toast";
Page({
  data: {
    isOrderList: true,
    tablist: [{
        value: "全部",
        index: 5,
      },
      {
        value: "待付款",
        index: 0,
      },
      {
        value: "待发货",
        index: 1,
      }, {
        value: "待收货",
        index: 2,
      }, {
        value: "退款中",
        index: 6,
      },
      {
        value: "已完成",
        index: 4,
      }, {
        value: "已退款",
        index: 3,
      }

    ],
    tabIndex: 0, //tab栏默认选中第一个
    orderList: [],
  },
  onLoad() {
    this.getIntegralUserOrder()
    wx.setStorageSync('add', 0)
  },
  // 取消订单
  delete(e) {
    let order_on = e.currentTarget.dataset.order
    app.http.post(app.api.IntegralDelete, {
      order_on
    }, (res) => {
      this.getIntegralUserOrder()
    })
  },
  //立即兑换
  buy(e) {
    wx.setStorageSync('add', 0)
    wx.setStorageSync('address', {
      id: 0
    })
    let order_on = e.currentTarget.dataset.order
    wx.navigateTo({
      url: '/pages/integral/exchange/index?order_on=' + order_on,
    })
  },
  // 确认收货
  receipt(e) {
    let order_on = e.currentTarget.dataset.order
    app.http.post(app.api.IntegralReceipt, {
      order_on
    }, (res) => {
      wx.showLoading({
        title: '加载中',
      })
      // 请求数据
      this.getIntegralUserOrder()
    })
  },

  // 申请退款
  refund(e) {
    const that = this
    let order_on = e.currentTarget.dataset.order
    app.http.post(app.api.IntegralRefund, {
      order_on
    }, (res) => {
      setTimeout(() => {
        Toast({
          type: 'success',
          message: res.message,
          forbidClick: true,
          onClose: () => {
            that.getIntegralUserOrder()
          },
        });
      }, 0)

    })
  },
  getIntegralUserOrder() {
    let status = this.data.tablist[this.data.tabIndex].index
    app.http.post(app.api.IntegralUserOrder, {
      status
    }, (res) => {
      wx.hideLoading()
      if (res.data == "") {
        this.setData({
          orderList: res.data,
          isOrderList: true
        })
      } else {
        this.setData({
          orderList: res.data,
          isOrderList: false
        })
      }
    })
  },
  // 切换tab栏
  chooseTab(e) {
    console.log(e);
    this.setData({
      tabIndex: e.currentTarget.dataset.id,
    });
    this.getIntegralUserOrder()
  },
});